import BaseXform from '../base-xform';

class FloatXform extends BaseXform {
    constructor(options) {
        super();

        this.tag = options.tag;
        this.attr = options.attr;
        this.attrs = options.attrs;
    }

    render(xmlStream, model) {
        if (model !== undefined) {
            xmlStream.openNode(this.tag);
            if (this.attrs) {
                xmlStream.addAttributes(this.attrs);
            }
            if (this.attr) {
                xmlStream.addAttribute(this.attr, model);
            } else {
                xmlStream.writeText(model);
            }
            xmlStream.closeNode();
        }
    }

    parseXmlObject(node) {
        if (node._name === this.tag) {
            if (this.attr) {
                this.model = parseFloat(node._attributes[this.attr], 10);
            } else {
                let val = ""
                node._elements?.forEach((item) => {
                    val = item._text
                })
                this.model = parseFloat(val || 0, 10);
            }
            return true;
        }
        return false;
    }

    parseOpen(node) {
        if (node.name === this.tag) {
            if (this.attr) {
                this.model = parseFloat(node.attributes[this.attr]);
            } else {
                this.text = [];
            }
        }
    }

    parseText(text) {
        if (!this.attr) {
            this.text.push(text);
        }
    }

    parseClose() {
        if (!this.attr) {
            this.model = parseFloat(this.text.join(''));
        }
        return false;
    }
}

export default FloatXform;
